package cn.soft863.servlet.head;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.soft863.bike.pojo.ABike;
import cn.soft863.bike.service.IBikeService;
import cn.soft863.bike.service.impl.BikeServiceImpl;
import cn.soft863.common.servlet.BaseServlet;
import cn.soft863.common.util.DateUtil;
import cn.soft863.common.util.PageInfoUtil;

/**
 * 车辆Servlet,前台
 * @author 902-B-Book
 *
 */
public class BikeHeadServlet extends BaseServlet
{
	private IBikeService bikeService = new BikeServiceImpl();
	private DateUtil dateUtil = new DateUtil();
	/**
	 * Service方法
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 车辆列表 */
		String method = request.getParameter("method");
		if("bikeList".equalsIgnoreCase(method))
		{
			bikeList(request,response);
		}
	}

	/**
	 * 车辆列表
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void bikeList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");

		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		/*
		 * currentPage如何木有传或者传入的不是整数,咋办? 转换数据会报错,咋办?
		 */
		try
		{
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (NumberFormatException e)
		{
		}

		/*
		 * 传搜索要用的
		 * 
		 * 凡是Servlet需要的东西,需要的参数,都问jsp要,都从request里面取
		 */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");

		if (keyword == null)
		{
			keyword = "";
		}
		/* 取掉前后空格 */
		keyword = keyword.trim();
		if (status == null)
		{
			status = "";
		}
		if (st == null)
		{
			st = "";
		}
		if (ed == null)
		{
			ed = "";
		}

		Date stDate = null;
		Date edDate = null;

		/* 如何将字符串的日期时间变成Date对象呢? */
		if (!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = dateUtil.strToDateTime(st);
			edDate = dateUtil.strToDateTime(ed);
		}

		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 按照关键字查询 */
		condMap.put("keyword", keyword);
		/* 按照状态查询 */
		condMap.put("status", 1);
		condMap.put("extends", "true");
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);

		/**
		 * 终于将查询的结果放到了集合中
		 */
		List<ABike> bikeList = this.bikeService.findCondListBikeService(pageInfoUtil, condMap);
		/* 将Service调用方法的返回值放到四大作用域中. */
		request.setAttribute("bikeList", bikeList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);

		/* 存储搜索条件 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);

		this.url = "/WEB-INF/jsp/head/bike/bikeList.jsp" ; 
		/* 服务器端跳转 */
		request.getRequestDispatcher(url).forward(request, response);
	}
}
